package io.renren.sf.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.entity.CbBookEntity;
import io.renren.sf.service.CbBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbContentsEntity;
import io.renren.sf.service.CbContentsService;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-08 23:20:11
 */
@RestController
@RequestMapping("sf/cbcontents")
public class CbContentsController {
    @Autowired
    private CbContentsService cbContentsService;
    @Autowired
    private CbBookService cbBookService;
    /**
     * 通过id获取bookId
     */
    @GetMapping("/getBookIdById/{id}")
    public R getBookIdById(@PathVariable("id") Long id){
        // 先看看是不是bookId
        CbBookEntity byId = cbBookService.getById(id);
        if(byId != null){
            // 说明是bookId
            return R.ok().put("bookId", id);
        }else {
            // 是contentsId，通过它获取bookId
            Long bookId = cbContentsService.getById(id).getBid();
            return R.ok().put("bookId", bookId);
        }
    }
    /**
     * 目录分页
     */
    @GetMapping("/list")
    public R contentsPage(@RequestParam Map<String, Object> params) {
        Page<CbContentsEntity> contentsPage = cbContentsService.getContentsByBookId(params);
        return R.ok().put("contentsPage", contentsPage);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbcontents:info")
    public R info(@PathVariable("id") Long id){
		CbContentsEntity cbContents = cbContentsService.getById(id);

        return R.ok().put("cbContents", cbContents);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbcontents:save")
    public R save(@RequestBody CbContentsEntity cbContents){
		cbContentsService.save(cbContents);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbcontents:update")
    public R update(@RequestBody CbContentsEntity cbContents){
		cbContentsService.updateById(cbContents);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbcontents:delete")
    public R delete(@RequestBody Long[] ids){
		cbContentsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
